
/**
 * 重写表单提交
 */
Ext.override(Ext.form.Action.Submit, {
	 decodeResponse: function(response) {
        try {
        	var rt = response.responseText;
        	return Ext.decode(rt.replace(/<\/?pre>/gi,''));
        } catch(e) {
        	return false;
        }
    }
});
/**
 * 重写BasicForm
 * @author zz
 * 
 */
Ext.override(Ext.form.BasicForm, {
	
	setValues : function(values){
		if(Ext.isArray(values)){ // array of objects
            for(var i = 0, len = values.length; i < len; i++){
                var v = values[i];
                var f = this.findField(v.id,true);//非原生方式
                if(f){
                    f.setValue(v.value);
                    if(this.trackResetOnLoad){
                        f.originalValue = f.getValue();
                    }
                }
            }
        }else{ // object hash
            var field, id;
            for(id in values){
            	if(!Ext.isFunction(values[id])){
            		if(field = this.findField(id,true)){
						field.setValue(values[id]);
					    if(this.trackResetOnLoad){
					        field.originalValue = field.getValue();
					    }
            		} else if(Ext.isObject(values[id])){//再深一层,entity.prop1.prop1props
						var val = values[id];
						for(var _id in val){
							if(field = this.findField(id+'.'+_id,true)){
								field.setValue(val[_id]);
								if(this.trackResetOnLoad){
			                        field.originalValue = field.getValue();
			                    }
							}
						}
            		}
				}
            }
        }
        return this;
    },
    findField : function(id,usedefine) {
    	if(usedefine){
    		id = (this.entityName==null?id:this.entityName+'.'+id);
    	
    	}
    	var field = this.items.get(id);
    	if (!Ext.isObject(field)) {
            //searches for the field corresponding to the given id. Used recursively for composite fields
            var findMatchingField = function(f) {
                if (f.isFormField) {
                    if (f.dataIndex == id || f.id == id || f.getName() == id) {
                        field = f;
                        return false;
                    } else if (f.isComposite) {
                        return f.items.each(findMatchingField);
                    } else if (f instanceof Ext.form.CheckboxGroup && f.rendered) {
                        return f.eachItem(findMatchingField);
                    }
                }
            };

            this.items.each(findMatchingField);
        }
        return field || null; 
	 }
});
/**
 * 重写文本框
 */
Ext.override(Ext.form.TextField, {
	initComponent : function(){
		if(!this.allowBlank&&!this.useOrigin){
    		this.fieldLabel = '<span style="color:red"> * </span>'+this.fieldLabel;
    	}
    	Ext.form.TextField.superclass.initComponent.call(this,arguments);     
    }
});

/**
 * 重写日期框
 */
Ext.override(Ext.form.DateField, {
	initComponent : function(){
		if(!this.allowBlank&&!this.useOrigin){
    		this.fieldLabel = '<span style="color:red"> * </span>'+this.fieldLabel;
    	}
    	Ext.form.TextField.superclass.initComponent.call(this,arguments);     
    },
    setValue : function(date){
    	var d = date;
    	if(!Ext.isDate(d)){
    		d = Date.parseDate(date,'Y-m-d H:i:s');
    	}
    	if(d!='undefined'&&d!=null){
    		d = d.dateFormat(this.format);
    	}
    	
    	return Ext.form.DateField.superclass.setValue.call(this, d);
    }
});




function checkException(response){
	var status = response.status;
	var text = response.responseText;
	switch (status) {
		case 404 :
			Ext.MessageBox.alert("错误", "加载数据时发生错误:请求路径不可用");
			return false;
		case 200 :
			if (text.length > 0) {
				var obj = Ext.decode(text);
				if (obj && !obj.success) {
					Ext.MessageBox.alert("错误", "加载数据时发生错误:<br/>" + obj.data);
					return false;
				}
			};return true;
		case 0 :
			Ext.MessageBox.alert("错误", "加载数据时发生错误:<br/>" + "远程服务器无响应");
			return false;
		default :
			var obj = Ext.decode(text);
			if (obj && !obj.success) {
				Ext.MessageBox.alert("错误", "加载数据时发生错误<br/>错误码:" + status
								+ "<br/>错误信息:" + obj.data);
				return false;
			}

			return true;
	}
	return true;
}

if (Ext.data.Store) {
	var _constructorFn = Ext.data.Store.prototype.constructor;
	Ext.data.Store.prototype.constructor = function(A) {
		_constructorFn.call(this, A);
		if (!this.hasListener('loadexception')) {
			this.on('loadexception', showExtLoadException);
		}
	}

	function showExtLoadException(This, options, response, error) {
		checkException(response);
	}
}

/**
 * 单元格复制
 */
if (!Ext.grid.GridView.prototype.templates) {
   Ext.grid.GridView.prototype.templates = {};
}
Ext.grid.GridView.prototype.templates.cell = new Ext.Template(
   '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}"  style="{style}" tabIndex="0" {cellAttr}>',
   '<div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{value}</div>',
   '</td>'
)